﻿Public Class frmChantiers

    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Private Sub frmRechercheChantier_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.BackColor = getSocieteColor()
        TE_NOM.Clear()
        TE_NOM.Focus()
        CB_TYPE.SelectedIndex = 0
        InitDossiers(0)
    End Sub

    Private Sub InitDossiers(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_DOSSIERS.Items.Clear()
        LV_DOSSIERS.ListViewItemSorter = Nothing

        ' lire toutes les dossiers du clients
        ' remplacer les virgules du nom par un blanc
        szSQL = "CALL spGET_DOSSIERS_byChantier('" & gSocieteCode & "','" & TE_NOM.Text.Replace(",", " ") & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        Do While Not rs.EOF
            LV_DOSSIERS.Items.Add(rs.Fields("type").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("date").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("numero").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("nomcli").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("chantier").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(CDec(rs.Fields("montant").Value).ToString("C"))
            LV_DOSSIERS.Items(nb).SubItems.Add(GetLibST(rs.Fields("status").Value))
            If rs.Fields("type").Value = "FA" Then
                LV_DOSSIERS.Items(nb).ForeColor = Color.Green
            End If
            rs.MoveNext()
            nb = nb + 1
        Loop
        rs.Close()

        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            m_SortingColumn.Text = m_SortingColumn.Text.Replace("> ", "")
            SortListView(LV_DOSSIERS, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_DOSSIERS)

    End Sub

    Private Sub TE_NOM_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_NOM.KeyDown
        If e.KeyCode = Keys.Enter Then
            Call InitDossiers(0)
        End If
    End Sub

    Private Sub LV_DOSSIERS_ColumnClick(sender As Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_DOSSIERS.ColumnClick
        SortListView(LV_DOSSIERS, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column
    End Sub

    Private Sub LV_DOSSIERS_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_DOSSIERS.DoubleClick

        If LV_DOSSIERS.SelectedItems.Count > 0 Then
            ' affiche les détails du dossier sélectionné
            Show_Dossier(LV_DOSSIERS.SelectedItems(0).SubItems(0).Text, LV_DOSSIERS.SelectedItems(0).SubItems(2).Text)
            ' réaffiche la viewlist
            InitDossiers(LV_DOSSIERS.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_DOSSIERS_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_DOSSIERS.KeyDown

        Select Case e.KeyCode
            Case Keys.Enter         ' Enter = DoubleClick
                LV_DOSSIERS_DoubleClick(sender, e)

        End Select

    End Sub

    Private Sub BO_ADD_LIGNE_Click(sender As System.Object, e As System.EventArgs) Handles BO_ADD_LIGNE.Click

        ' création dossier
        TE_NOM.Text = Show_Dossier(getCodeFromCombo(CB_TYPE.SelectedItem), 0)
        ' réaffiche la viewlist
        InitDossiers(0)

    End Sub

 End Class